package com.jingyanzi.resallocation.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.jingyanzi.resallocation.domain.QueryRequest;
import com.jingyanzi.resallocation.domain.RowRequest;
import com.jingyanzi.resallocation.domain.SelectDO;
import com.jingyanzi.resallocation.domain.SingleRequest;
import com.jingyanzi.resallocation.domain.SiteDO;
import com.jingyanzi.resallocation.query.SiteQueryDO;
import com.jingyanzi.resallocation.result.ListResponse;
import com.jingyanzi.resallocation.result.PageResponse;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.result.SingleResponse;
import com.jingyanzi.resallocation.service.ISiteService;
import com.jingyanzi.resallocation.util.EncryptedUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(description = "场景接口")
@RestController
@RequestMapping("/")
public class SiteController {
	
	public static final String SYS_ERROR = "{status:500, msg:\"系统错误\", version:1}";

	public static final String DES = "3DES";
	
	public static final String PLANINTEXT = "plaintext";

	@Autowired
	private ISiteService siteService;
	
	public static String encodeObject(SingleResponse<SiteDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeList(ListResponse<SiteDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePage(PageResponse<SiteDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static SingleRequest<SiteDO> decodeObject(String encodedText, String encryptType) {
		Gson gson = new Gson();
		SingleRequest<SiteDO> request;
		try {
			request = gson.fromJson(EncryptedUtil.decrypt(encodedText, encryptType),
					new TypeToken<SingleRequest<SiteDO>>() {
					}.getType());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return request;
	}

	public static RowRequest<SiteDO> decodeArray(String encodedText, String encryptType) {
		Gson gson = new Gson();
		RowRequest<SiteDO> request;
		try {
			request = gson.fromJson(EncryptedUtil.decrypt(encodedText, encryptType),
					new TypeToken<RowRequest<SiteDO>>() {
					}.getType());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return request;
	}

	public static QueryRequest<SiteQueryDO> decodeQuery(String encodedText, String encryptType) {
		Gson gson = new Gson();
		QueryRequest<SiteQueryDO> request;
		try {
			request = gson.fromJson(EncryptedUtil.decrypt(encodedText, encryptType),
					new TypeToken<QueryRequest<SiteQueryDO>>() {
					}.getType());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return request;
	}
	
	@ApiOperation(value = "新增场景" ,  notes="新增场景")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "siteName", value = "场景名称", required = true, dataType = "String"),
        @ApiImplicitParam(name = "sceneType", value = "场景类型", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "sceneClass", value = "场景等级", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "region", value = "区域编号", required = true, dataType = "String"),
        @ApiImplicitParam(name = "address", value = "地址", required = true, dataType = "String"),
        @ApiImplicitParam(name = "tel", value = "电话", required = true, dataType = "String"),
        @ApiImplicitParam(name = "remark", value = "备注", required = true, dataType = "String"),
    })
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public String add(@RequestBody SingleRequest<SiteDO> request) {

		SiteDO siteDO = request.getData();
		
		SingleResponse<SiteDO> response = new SingleResponse<SiteDO>();
		System.out.println(siteDO);
		ResultDO result = siteService.add(siteDO);
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
			
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}

		return encodeObject(response, PLANINTEXT);
	}
	
	@ApiOperation(value = "修改场景" ,  notes="修改场景")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "场景Id", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "siteName", value = "场景名称", required = true, dataType = "String"),
        @ApiImplicitParam(name = "sceneType", value = "场景类型", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "sceneClass", value = "场景等级", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "region", value = "区域编号", required = true, dataType = "String"),
        @ApiImplicitParam(name = "address", value = "地址", required = true, dataType = "String"),
        @ApiImplicitParam(name = "tel", value = "电话", required = true, dataType = "String"),
        @ApiImplicitParam(name = "remark", value = "备注", required = true, dataType = "String"),
    })
	@RequestMapping(value = "/edit.json", method = RequestMethod.POST)
	public String edit(@RequestBody SingleRequest<SiteDO> request) {

		SiteDO siteDO = request.getData();
		
		SingleResponse<SiteDO> response = new SingleResponse<SiteDO>();
		
		ResultDO result = siteService.edit(siteDO);
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
			
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}

		return encodeObject(response, PLANINTEXT);
	}
	
	@ApiOperation(value = "删除场景" ,  notes="删除场景")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "场景Id", required = true, dataType = "Integer"),
    })
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public String delete(@RequestBody SingleRequest<SiteDO> request) {

		SiteDO siteDO = request.getData();
		
		SingleResponse<SiteDO> response = new SingleResponse<SiteDO>();
		
		ResultDO result = siteService.remove(siteDO.getId());
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
			
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}

		return encodeObject(response, PLANINTEXT);
	}
	
	@ApiOperation(value = "获取场景" ,  notes="获取场景")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "场景Id", required = true, dataType = "Integer"),
    })
	@RequestMapping(value = "/detail.json", method = RequestMethod.POST)
	public String detail(@RequestBody SingleRequest<SiteDO> request) {

		SiteDO siteDO = request.getData();
		
		SingleResponse<SiteDO> response = new SingleResponse<SiteDO>();
		
		ResultDO result = siteService.get(siteDO.getId());
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setData((SiteDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			response.setMsg("成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("失败");
		}

		return encodeObject(response, PLANINTEXT);
	}
	
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<SiteQueryDO> request) {

		SiteQueryDO query = request.getData();
		
		query.setPage(true);
		PageResponse<SiteDO> response = new PageResponse<SiteDO>();
		ResultDO result = siteService.query(query);
	
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setData((List<SiteDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			response.setMsg("成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("失败");
		}

		return encodePage(response, PLANINTEXT);
	}
	
	@RequestMapping(value = "/select.json", method = RequestMethod.POST)
	public String select(@RequestBody QueryRequest<SiteQueryDO> request) {

		SiteQueryDO query = request.getData();
		
		query.setPage(false);
		ListResponse<SelectDO> response = new ListResponse<SelectDO>();
		ResultDO result = siteService.query(query);
		if (result.isSuccess()) {
			List<SiteDO> list = (List<SiteDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			List<SelectDO> selectList = new ArrayList<SelectDO>();

			for (SiteDO domain : list) {
				SelectDO selectDO = new SelectDO();
				selectDO.setValue(domain.getId());
				selectDO.setLabel(domain.getSiteName());
				selectList.add(selectDO);
			}
			response.setData(selectList);
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("失败");
		}

		Gson gson = new Gson();

		return gson.toJson(response);
	}
	
	
}
